home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech® Magazine / Volume 11 - 1995 / 11.02 Feb 95 / LowPriorityEvents < prev   
Encoding:
Text File  |  1996-04-04  |  3.4 KB  |  85 lines  |  [TEXT/R*ch]

  1. Using Low Priority Events in MacApp
  2. code snippets which accompany the article
  3. © 1995, MacTech Magazine and Harry Haddon.
  4.  
  5. The priorities defined by MacApp are:  
  6.  
  7. // Low priority commands are considered last
  8. const short kPriorityLowest = 127;
  9. const short kPriorityLow = kPriorityLowest - 32;
  10. // Normal priority: command default priority
  11. const short kPriorityNormal = 64;
  12. const short kPriorityHigh = kPriorityNormal - 32;
  13. //High priority commands take precedence
  14. const short kPriorityHighest = 0;
  15.  
  16.  
  17. I changed the priority of TEventRetrieverCommand in IMyApplication() after calling IApplication():
  18.  
  19. TEventRetrieverCommand *originalEventRetriever;
  20. originalEventRetriever = 
  21.               (TEventRetrieverCommand *) fEventList->At(1);
  22. originalEventRetriever->fPriority = kPriorityLowest;
  23.  
  24. if (qDebug && !originalEventRetriever->IsMemberClass(
  25.              GetClassIDFromName("TEventRetrieverCommand")))
  26.       ProgramBreak("First command in fEventList is not \
  27. a TEventRetrieverCommand!");
  28.  
  29.  
  30. To keep processing toolbox events at kPriorityLow, I declared a new command that is a descendant of TEventRetrieverCommand.  This command checks for toolbox events but never sleeps.  It is posted at kPriorityLow to replace the original TEventRetrieverCommand that was demoted to kPriorityLowest.  
  31.  
  32. class TNoSleepEventRetrieverCommand : 
  33.                             public TEventRetrieverCommand {
  34. public:
  35.   TNoSleepEventRetrieverCommand();
  36.     // Empty constructor to satisfy compiler.  
  37.  
  38.   virtual pascal void INoSleepEventRetrieverCommand(
  39.                       CommandNumber itsCommandNumber); 
  40.     // Initialize the EventCommand procedurally.  
  41.   virtual pascal Boolean IsReadyToExecute();                                 // override 
  42.     // Return true when event available
  43.  
  44.     virtual pascal void DoIt();
  45.     // Retrieve and process an event without sleeping 
  46. };
  47.  
  48. I put the declaration for TNoSleepEventRetrieverCommand in the header file that contains the declaration for TMyApplication.
  49. I put the definitions for its methods in the .cp file that contains the methods of TMyApplication.  The initialization method INoSleepEventRetrieverCommand() just calls IEventRetrieverCommand() and then sets the command's priority:
  50.  
  51. #pragma segment ASelCommand
  52. pascal void TNoSleepEventRetrieverCommand::INoSleepEventRetrieverCommand(
  53.     CommandNumber itsCommandNumber) 
  54. {
  55.   this->IEventRetrieverCommand(itsCommandNumber); 
  56.  
  57.     // Let more important stuff happen first
  58.   fPriority = kPriorityLow;
  59. }
  60.  
  61. Its IsReadyToExecute method returns true whenever a toolbox event is available: 
  62.  
  63. #pragma segment ARes
  64. pascal Boolean TNoSleepEventRetrieverCommand::IsReadyToExecute() 
  65. {
  66.     EventRecord theEvent;
  67.     
  68.     return EventAvail(gApplication->fMainEventMask, theEvent); 
  69. }
  70. When IsReadyToExecute() returns true, MacApp calls the command’s DoIt() method.  The DoIt() for TNoSleepEventRetrieverCommand is just like DoIt() for TEventRetrieverCommand except it calls PollToolboxEvent() with the parameter allowApplicationToSleep set to false so the application doesn’t go to sleep on us:
  71.  
  72. #pragma segment ASelCommand
  73. pascal void TNoSleepEventRetrieverCommand::DoIt() 
  74. {
  75.     gApplication->PollToolboxEvent(FALSE);    
  76.     // FALSE = never sleep
  77. }
  78.  
  79. The TNoSleepEventRetrieverCommand is created and posted in TMyApplication after the priority of the original TEventRetrieverCommand is changed:
  80.  
  81. TNoSleepEventRetrieverCommand *aEventCommand = 
  82.                          new TNoSleepEventRetrieverCommand;
  83. aEventCommand->INoSleepEventRetrieverCommand(cNoCommand); 
  84. this->PostAnEvent(aEventCommand);
  85.